//
//  QLGiftMallController.m
//  神约
//
//  Created by qlwl on 17/2/6.
//  Copyright © 2017年 qlwl. All rights reserved.
//

#import "QLGiftMallController.h"
#import "QLBuyGiftView.h"
#import "QLGiftMallListModel.h"
#import "QLGiftMallListCell.h"

#define ItemSpace 8
#define CellWidth (iphoneWidth - (3 + 1)*ItemSpace)/3
#define CellHeight (CellWidth*4/3)

@interface QLGiftMallController ()
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation QLGiftMallController
{
    NSArray * _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavTitle:@"赠送礼物"];
    ((UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout).minimumLineSpacing = ItemSpace;
    ((UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout).minimumInteritemSpacing = ItemSpace;
    
    [self netWorking];
}

#pragma mark CollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    QLGiftMallListCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QLGiftMallListCell" forIndexPath:indexPath];
    QLGiftMallListModel * model = _dataArray[indexPath.row];
    [cell.giftIma QL_setImageWithURL:[NSString stringWithFormat:@"%@%@",HTTP_BaseURL,model.picurl] placeholderImage:nil];
    cell.nameLab.text = model.giftname;
    cell.needGoldLab.text = [NSString stringWithFormat:@"金币 %d",model.price];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(CellWidth, CellHeight);
    
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(ItemSpace,ItemSpace,ItemSpace,ItemSpace);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.sendUser = @"3";
    QLGiftMallListModel * model = _dataArray[indexPath.row];
    __weak typeof(model) w_model = model;
    __weak typeof(self) w_self = self;
    [QLBuyGiftView showInWindowBuyNum:^(int giftNum) {
        [self sendFriend:w_self.sendUser gift:w_model.id andGiftNum:giftNum];
    }];
}
- (BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    return YES;
}

#pragma mark netWorking
- (void) netWorking
{
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc] init];
    postDict[@"gifttype"] = @"-1";
    [QLNetWorking postJSONWithUrl:QLGiftMallListURL parameters:postDict isJosnRequest:YES rogress:nil success:^(id result) {
        NSLog(@"%@",result);
        if ([QLNetWorking sucess:[result objectForKey:@"mobileHead"] and:@"code" EqualToString:@"success"]) {
            NSArray * array = [QLGiftMallListModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"mobileBody"] objectForKey:@"data"]];
            _dataArray = array;
            [self.collectionView reloadData];
        }
    } fail:^(NSError *error) {
        
    }];
}

- (void) sendFriend:(NSString *)fridendId gift:(NSString *)giftId andGiftNum:(int)giftNum
{
    QLLoginModel * loginModel = [QLLoginModel accountFromSandbox];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc] init];
    postDict[@"giftid"] = giftId;
    postDict[@"giftnum"] = [NSNumber numberWithInt:giftNum];
    postDict[@"receiveuserid"] = fridendId;
    postDict[@"userid"] = loginModel.userId;
    
    [QLNetWorking postJSONWithUrl:QLSendFriendGiftURl parameters:postDict isJosnRequest:YES rogress:nil success:^(id result) {
        NSLog(@"%@",result);
        if ([QLNetWorking sucess:[result objectForKey:@"mobileHead"] and:@"code" EqualToString:@"success"]) {
            NSLog(@"%@",result);
        }
        [LPPopup showInCenterOfWindowWithText:[[result objectForKey:@"mobileHead"] objectForKey:@"message"] completion:nil];
    } fail:^(NSError *error) {
        
    }];
}

@end
